package sample.tools;

import java.util.regex.Pattern;

public class YUtils {
    /**
     * 将 GPGGA的经纬度数据转换为 GPS经纬度
     * @param longitude 经度
     * @param latitude 纬度
     * @return double[]{经度, 纬度}
     */
    public static double[] GPGGALongLatConvert(String longitude, String latitude){
        double var52 = (double)((int)(Double.parseDouble(longitude) / (double)100));
        double var50 = Double.parseDouble(longitude);
        final double x = var52 + (var50 - (double)((int)(Double.parseDouble(longitude) / (double)100)) * (double)100) / (double)60;
        var52 = (double)((int)(Double.parseDouble(latitude) / (double)100));
        var50 = Double.parseDouble(latitude);
        final double y = var52 + (var50 - (double)((int)(Double.parseDouble(latitude) / (double)100)) * (double)100) / (double)60;
        return new double[]{x, y};
    }
    /**
     * 方法二：推荐，速度最快
     * 判断是否为整数
     * @param str 传入的字符串
     * @return 是整数返回true,否则返回false
     */
    public static boolean isInteger(String str) {
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*[\r\n]?[\r]?[\n]?$");
        return pattern.matcher(str).matches();
    }

    public String[] commSplitString(String str){
        String[] strArr = str.split(",");
            return strArr;
    }

    public String[] nbspSplitString(String str){
        String[] strArr = str.split(" ");
        return strArr;
    }

    public String[] hexCommSplitString(String str){
        String[] strArr = str.split("2C");
        return strArr;
    }

    /**
     * hex转byte数组
     * @param hex
     * @return
     */
    public static byte[] hexToByte(String hex){
        int m = 0, n = 0;
        int byteLen = hex.length() / 2; // 每两个字符描述一个字节
        byte[] ret = new byte[byteLen];
        for (int i = 0; i < byteLen; i++) {
            m = i * 2 + 1;
            n = m + 1;
            int intVal = Integer.decode("0x" + hex.substring(i * 2, m) + hex.substring(m, n));
            ret[i] = Byte.valueOf((byte)intVal);
        }
        return ret;
    }
    /**
     * byte数组转hex
     * @param bytes
     * @return
     */
    public static String byteToHex(byte[] bytes){
        String strHex = "";
        StringBuilder sb = new StringBuilder("");
        for (int n = 0; n < bytes.length; n++) {
            strHex = Integer.toHexString(bytes[n] & 0xFF);
            sb.append((strHex.length() == 1) ? " 0" + strHex : " "+strHex); // 每个字节由两个字符表示，位数不够，高位补0
        }
        return sb.toString().trim();
    }
    /**
     * 获得比例
     * @param num 实际数
     * @param maxNum 展示最大数
     * @return 获得比例
     */
    public double getRatio(double num, double maxNum){
        return (double)num/maxNum;
    }
    public double getRatio(double num, int maxNum){
        return (double)num/maxNum;
    }
    public double getRatio(int num, double maxNum){
        return (double)num/maxNum;
    }
    public double getRatio(int num, int maxNum){
        return (double)num/maxNum;
    }

    /**
     * 转换坐标
     * @param num 实际数
     * @param ratioNum 比例
     * @return 转换后的数
     */
    public double AutoAdaptWorkSpace(double num, double ratioNum){
        return (double)num/(ratioNum);
    }
    public double AutoAdaptWorkSpace(double num, int ratioNum){
        return (double)num/(ratioNum);
    }
    public double AutoAdaptWorkSpace(int num, double ratioNum){
        return (double)num/(ratioNum);
    }
    public double AutoAdaptWorkSpace(int num, int ratioNum){
        return (double)num/(ratioNum);
    }
}
